
package com.hd.sup.modules.alarm.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.common.constant.BizConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.GenUtil;
import com.hd.common.utils.Pair;
import com.hd.sup.modules.alarm.model.AlarmRule;
import com.hd.sup.modules.alarm.service.IAlarmRuleService;
import com.hd.sup.modules.alarm.service.IAlarmRuleVehicleService;
import com.hd.sup.modules.alarm.wrapper.AlarmRuleWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title AlarmRuleController.java
 * @Description 报警管理-规则配置控制器
 * @Package com.hd.sup.modules.alarm.controller
 * @date 2021/1/7 6:47 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/alarmRule")
public class AlarmRuleController extends BaseController {

    @Resource
    private IAlarmRuleService alarmRuleService;

    @Resource
    private IAlarmRuleVehicleService alarmRuleVehicleService;

    @PostMapping("/list")
    public Result<?> list(@RequestBody RequestData requestData) {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String ruleName = requestData.getString("ruleName");
        String alarmType = requestData.getString("alarmType");
        //入参校验
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");
        if (StringUtils.isBlank(alarmType)) return Result.error("参数 alarmType 为空");

        AlarmRule param = new AlarmRule(ruleName, alarmType);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = (List<Map<String, Object>>) new AlarmRuleWrapper(alarmRuleService.pageList(page, param)).warp();
        page.setRecords(result);
        return Result.success(page);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 删除规则信息
     * @date 2020/12/31 3:25 下午
     * @params [ids]
     */
    @DeleteMapping("/delete/{ids}")
    public Result<?> delete(@PathVariable() String ids) {
        if (StringUtils.isBlank(ids)) return Result.error("参数 ids 为空");
        alarmRuleService.removeByIds(Arrays.asList(ids.split(BizConstant.Common.SEPARATOR.key())));
        return Result.success();
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 保存或更新
     * @date 2020/12/31 3:37 下午
     * @params [ids]
     */
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(AlarmRule alarmRule) {
        //入参校验
        if (alarmRule == null) return Result.error("参数为空");

        String startTimeStr = alarmRule.getStartTimeStr();
        String endTimeStr = alarmRule.getEndTimeStr();
        if (StringUtils.isNotBlank(startTimeStr))
            alarmRule.setStartTime(DateUtil.parse(startTimeStr, BizConstant.Common.DATE_FORMAT_HMS.key()));
        if (StringUtils.isNotBlank(endTimeStr))
            alarmRule.setEndTime(DateUtil.parse(endTimeStr, BizConstant.Common.DATE_FORMAT_HMS.key()));

        if (StringUtils.isBlank(alarmRule.getId())) {
            alarmRule.setId(GenUtil.newId());
            alarmRule.setCreateBy(getCurrentUser().getUsername());
        }
        alarmRule.setUpdateBy(getCurrentUser().getUsername());
        alarmRule.insertOrUpdate();
        return Result.success(alarmRule);
    }

    /**
     * @return com.hd.common.reqres.response.Result<?>
     * @author chentl
     * @description 取消绑定 （废弃）
     * @date 2021/3/8 3:02 下午
     * @params [requestData]
     */
    @PostMapping("/unBind")
    public Result<?> unDistributeVehicle(@RequestBody RequestData requestData) {
        String ruleId = requestData.getString("ruleId");
        String transType = requestData.getString("transType");
        BizConstant.RuleTransType ruleTransType = BizConstant.RuleTransType.get(transType);

        //valid 1 参数不为空
        if (StringUtils.isAnyEmpty(transType, ruleId)) return Result.error("参数中 transType 或 ruleId 为空");
        //valid 2 规则运输类型必须合法
        if (Objects.isNull(ruleTransType)) return Result.error("参数中 transType 不合法");

        alarmRuleVehicleService.deleteVehicleRules(ruleId, transType);
        return Result.success(200, "取消绑定成功", null);
    }

    /**
     * @return com.hd.common.reqres.response.Result<?>
     * @author chentl
     * @description 绑定 报警规则 给 车辆运输类型
     * @date 2021/3/17 2:56 下午
     * @params [requestData]
     */
    @PostMapping("/bind")
    public Result<?> bindTransType(@RequestBody RequestData requestData) {
        String ruleId = requestData.getString("ruleId");
        String transType = requestData.getString("transType");
        BizConstant.RuleTransType ruleTransType = BizConstant.RuleTransType.get(transType);
        //valid 2 规则运输类型必须合法
        if (Objects.isNull(ruleTransType)) return Result.error("参数中 transType 不合法");

        //valid 1 参数不为空
        if (StringUtils.isAnyEmpty(transType, ruleId)) return Result.error("参数中 transType 或 ruleId 为空");

        Pair<String, String> pair = alarmRuleVehicleService.bindTransType(ruleId, ruleTransType);
        String error = pair.first;
        if (StringUtils.isNotEmpty(error)) return Result.error(error);

        return Result.success(200, "【" + ruleTransType.val() + "】 类型绑定成功!", ruleId);
    }

}
